# @Timer:2022/3/29
# @作者:meng
# @文件名:conn.py
from mysql_connet import get_conn

# #1、创建连接
# conn = get_conn()
# #2、获取游标
# cursor = conn.cursor()
# #3、执行sql语句
# sql1 = "show databases;"
# # sql2 = "select version();"
# cursor.execute(sql1)
# #4、获取结果
# reuslt = cursor.fetchall()
# print(reuslt)
# #5、关闭连接
# conn.close()

'''mysql操作'''
#1、创建表
# def test_create():
#     conn = get_conn()
#     cursor = conn.cursor()
#     sql1 = """CREATE TABLE `student2` (
#   `id` int(11) DEFAULT NULL,
#   `name` varchar(50) DEFAULT NULL,
#   `age` char(20) DEFAULT NULL,
#   `birthday` date DEFAULT NULL
# ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"""
#     cursor.execute(sql1)
#     cursor.close()
#2、插入数据
def test_create():
    conn = get_conn()
    cursor = conn.cursor()
    # sql1 = """
    #     insert into student2 values(1,'zhangsan',18,'2020-10-10';);
    # """
    sql1 = "INSERT INTO `student2` VALUES ('11', 'ab', '118', '2022-03-30')";
    sql2 = "select * from student2";
    try:
        cursor.execute(sql1)
        conn.commit()
        print("插入成功")
        cursor.execute(sql2)
        result = cursor.fetchall()
        print(result)
        print("查询成功")
    except:
        conn.rollback()
        print("回滚")
    finally:
        conn.close()
        print("关闭连接")